<template>
  <div>
    <el-col :xs="24" :sm="12" :lg="12" class="margin-bottom">
      <el-card class="box-card">
        <div class="chooseDateTitle">
          <div class="chooseDate">
            <p
              v-for="(item,index) in choose"
              :key="index"
              @click="chooseDate(index)"
              :class="chav.id==item.id?'active':''"
            >{{item.text}}</p>
          </div>
          <p>各区{{chav.text}}运单情况</p>
        </div>
        <echartscom :echartObj="barObj" :style="{width: '100%', height: '200px'}" />
      </el-card>
    </el-col>
    <el-col :xs="24" :sm="12" :lg="12" class="margin-bottom">
      <el-card class="box-card">
        <Outside :title="'月度称重数量走势'">
          <echartscom :echartObj="lineObj" :style="{width: '100%', height: '200px'}" />
        </Outside>
      </el-card>
    </el-col>
  </div>
</template>
<script>
import echartscom from "./echartscom.vue";

import Outside from "./outside";
import { getStatisticsWaybill, getRankingWaybill } from "@/api/index.js";
export default {
  components: {
    echartscom,
    Outside,
  },
  data() {
    return {
      choose: [
        { id: 1, text: "近30天" },
        { id: 2, text: "本月" },
        { id: 3, text: "上月" },
        { id: 4, text: "本年" },
      ],
      chav: {id: 1,text:"近30天" },//选中的月份
      areaName:[], //barchart X轴
      areaNum:[], //barchart y轴
      mounth: [],//linechart x轴
      mounthNum: [], //linechart y轴
      barObj: {},  //bar
      lineObj: {}, //折线图
    };
  },
  created() {
    this.getStatisticsWaybillData();
    this.getRankingWaybillData()
  },
  methods: {
    //月份切换
    chooseDate(index) {
      this.chav = this.choose[index];
      this.getRankingWaybillData()
    },
    //月度称重
    getStatisticsWaybillData() {
      let that = this;
      getStatisticsWaybill()
        .then((response) => {
          if (response.code == 0) {
            response.data.forEach((item) => {
              that.mounth.push(item.month);
              that.mounthNum.push(item.totalWeight);
            });
            this.lineObj = {
              color: ["#3398DB"],
              series: [
                {
                  type: "line",
                  data: that.mounthNum,
                },
              ],
              tooltip: {
                trigger: "axis",
                axisPointer: {
                  // 坐标轴指示器，坐标轴触发有效
                  type: "shadow", // 默认为直线，可选为：'line' | 'shadow'
                },
              },
              grid: {
                top: "5%",
                left: "10%",
                right: "4%",
                bottom: "10%",
              },
              xAxis: {
                type: "category",
                splitLine: { show: false },
                data: that.mounth,
                 interval: 0,
                 axisTick: {
                  alignWithLabel: true,
                },
              },
              yAxis: {
                type: "value",
                minorTick: {
                  show: true,
                },
                minorSplitLine: { show: true },
              },
            };
          } else {
            console.log(response);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
   //称重以及运单情况
   getRankingWaybillData(){
      let that = this;
      getRankingWaybill(this.chav.id).then(response =>{
         if(response.code==0){
           that.areaName=[]
           that.areaNum=[]
           response.data.forEach(item=>{
             that.areaName.push(item.areaName)
             that.areaNum.push(item.uploadNum)
           })
           that.barObj={
                     color: ["pink"],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "shadow", // 默认为直线，可选为：'line' | 'shadow'
          },
        },
        grid: {
          top: "5%",
          left: "6%",
          right: "4%",
          bottom: "10%",
        },
        xAxis: [
          {
            type: "category",
            data: that.areaName,
            interval: 0,
            axisTick: {
              alignWithLabel: true,
            },
          },
        ],
        yAxis: [
          {
            type: "value",
          },
        ],
        series: [
          {
            type: "bar",
            barWidth: "40%",
            data: that.areaNum,
            itemStyle: {
              normal: {
                //这里是重点
                color: function (params) {
                  //注意，如果颜色太少的话，后面颜色不会自动循环，最好多定义几个颜色
                  var colorList = [
                    "#c23531",
                    "#2f4554",
                    "#61a0a8",
                    "#d48265",
                    "#91c7ae",
                    "#749f83",
                    "#ca8622",
                  ];
                  return colorList[params.dataIndex];
                },
              },
            },
          },
        ],
           }
         }
      }).catch(err =>{
        console.log(err)
      })
   } 
 },
};
</script>
<style lang="scss"  scoped>
p {
  margin: 0;
}
.margin-bottom {
  margin-bottom: 10px;
}
.chooseDate {
  display: flex;

  p {
    margin-right: 20px;
    font-size: 14px;
    cursor: pointer;
    height: 20px;
    line-height: 20px;
  }
}
.chooseDateTitle {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 14px;
  line-height: 20px;
  padding-bottom: 15px;
  P:nth-child(2){
    font-size:14px;
  }
}
.active {
  color:rgb(18, 116, 235);
}
.el-col:nth-child(1) {
  padding-right: 6px !important;
}
.el-col:nth-child(2) {
  padding-left: 6px !important;
}
@media screen and (max-width: 768px) {
  .el-col:nth-child(1) {
    padding-right: 12px !important;
  }
  .el-col:nth-child(2) {
    padding-left: 12px !important;
  }
}
</style>